package com.colin.dubbo.business.response;

import com.colin.dubbo.entity.dto.RequestDto;
import com.colin.dubbo.enums.ResponseStatusEnum;
import com.colin.dubbo.util.BeanUtils;
import com.colin.dubbo.util.StringUtil;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用响应类
 */
public class CommonResponse implements Serializable {
    private static final long serialVersionUID = 7275466686061200436L;
    
    private String status;
    private String message;
    private Map<String, Object> data = new HashMap<>();
    
    /**
     * 通用成功返回对象
     */
    public static CommonResponse success(){
        return success(null);
    }

    /**
     * 指定成功消息
     * @param message 提示消息
     */
    public static CommonResponse success(String message){
        CommonResponse response = new CommonResponse();
        response.setStatus(ResponseStatusEnum._200.getStatus());
        response.setMessage(!StringUtil.isBlank(message) ? message : ResponseStatusEnum._200.getDesc());
        return response;
    }

    /**
     * 设置返回数据
     * 返回尽量单独设置，不封装到固有状态中，以方便后续扩展
     * @param key 数据key
     * @param value 数据value
     */
    public void putData(String key, Object value){
        this.data.put(key, value);
    }

    /**
     * 设置返回数据
     * 对于直接返回对象的操作
     * 且此对象为RequestDto请求数据的子类
     */
    public void setData(RequestDto data) throws Exception{
        if(data != null){
            this.data = BeanUtils.bean2Map(data);
        }
    }

    /**
     * 返回系统异常
     */
    public void toSysError(){
        this.status = ResponseStatusEnum._500.getStatus();
        this.message = ResponseStatusEnum._500.getDesc();
    }

    /**
     * 返回业务异常
     * @param status 异常标识
     * @param message 异常说明
     */
    public void toBusiError(String status, String message){
        this.status = status;
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }
}
